import QtQuick 2.15
import QtQuick.Controls 2.15

Popup {
    id: cPopup
    x: popupX
    y: popupY + ( ( modal ) ? ( animationYOffset ) : ( 0 ) )
    padding: 0
    focus: true
    //closePolicy: ( modal ) ? ( Popup.NoAutoClose ) : ( Popup.CloseOnPressOutsideParent )

    readonly property real popupX: ( parent.width - width ) / 2
    readonly property real popupY: ( parent.height - height ) / 2

    property real animationYOffset: 0

    property real backgroundOpacity: 1

    onClosed: {
        if ( cPopup && cPopup.modal )
        {
            destroy();
        }
    }

    enter: Transition {

        NumberAnimation {
            property: "opacity"
            from: 0.0
            to: 1.0
            duration: ( modal ) ? ( 200 ) : ( 100 )
        }

        NumberAnimation {
            target: cPopup
            property: "animationYOffset"
            easing.type: Easing.OutQuart
            from: -30
            to: 0
            duration: 200
        }
    }

    background: Item {

        BorderImage {
            anchors.fill: parent
            anchors.leftMargin: -35
            anchors.rightMargin: -35
            anchors.topMargin: -28
            anchors.bottomMargin: -43
            border.left: 35
            border.right: 35
            border.top: 28
            border.bottom: 43
            opacity: backgroundOpacity
            source: "qrc:/images/control/Background.png"
        }
    }

    Overlay.modal: Rectangle{
        anchors.fill: parent
        color: Qt.rgba( 0, 0, 0, cPopup.opacity * 0.3 )
    }

    // 主窗口关闭时也关闭Popup，避免属性绑定报错
    Connections {
        target: CGlobal.window

        function onClosing() {
            cPopup.close();
        }
    }
}
